<?php

namespace Helpers;

use Phalcon\Di;

class Assets
{

    /**
     * 返回assets对象
     *
     * @return \Phalcon\Assets\Manager
     *
     * create by ck 20170409
     */
    private static function getAssets()
    {
        static $assets;
        if (!$assets) {
            $assets = Di::getDefault()->get('assets');
        }
        return $assets;
    }

    /**
     * 获取当前models名
     *
     * @return mixed
     *
     * create by ck 20170815
     */
    private static function getModuleName()
    {
        static $dispatcher;
        if (!$dispatcher) {
            $dispatcher = Di::getDefault()->get('dispatcher');
        }
        return $dispatcher->getModuleName();
    }

    public static function importMainJs($jsName)
    {
        $jsName and self::getAssets()->addJs($jsName . '?ver=' . VER);
    }

    public static function importJs($jsName)
    {
        if ($jsName) {
            $jsName = self::getModuleName() . '/' . $jsName;
//            $info = pathinfo($jsName);
//            if (substr($info['filename'], -4) != '.min') {
//                $newName = Tool::buildPath($info['dirname'], $info['filename'] . '.min.' . $info['extension']);
//                file_exists($newName) and $jsName = $newName;
//            }
            $jsName and self::getAssets()->addJs($jsName . '?ver=' . VER);
        }
    }

    public static function importMainCss($cssName)
    {
        $cssName and self::getAssets()->addCss($cssName);
    }

    public static function importCss($cssName)
    {
        $cssName and $cssName = self::getModuleName() . '/' . $cssName;
        $cssName and self::getAssets()->addCss($cssName);
    }

    public static function select2($jsName = null)
    {
        self::getAssets()
            ->addCss('assets/select2/select2.min.css')
            ->addJs('assets/select2/select2.min.js');
        self::importJs($jsName);
    }

    public static function easyUI($jsName = null, $detail = false)
    {
        self::getAssets()
            ->addCss('assets/easyui/themes/metro/easyui.css')
            ->addJs('assets/easyui/jquery.easyui.min.js')
            ->addJs('assets/easyui/easyui-lang-zh_CN.js')
            ->addJs('assets/xh/MainFun.js?ver=' . VER)
            ->addJs('assets/xh/OvertFun.js?ver=' . VER);
        if ($detail) {
            self::getAssets()
                ->addJs('assets/easyui/datagrid-detailview.js');
        }
        self::importJs($jsName);
    }

    public static function fileInput($jsName = null)
    {
        self::getAssets()
            ->addCss('assets/fileinput/fileinput.css')
            ->addJs('assets/fileinput/fileinput.js')
            ->addJs('assets/fileinput/fileinput-zh.js');
        self::importJs($jsName);
    }

    public static function datetimePicker($jsName = null)
    {
        self::getAssets()
            ->addCss('assets/bootstrap/bootstrap-datetimepicker.css')
            ->addJs('assets/bootstrap/bootstrap-datetimepicker.js')
            ->addJs('assets/bootstrap/bootstrap-datetimepicker.zh-CN.js');
        self::importJs($jsName);
    }

    public static function UMEditor($jsName = null)
    {
        self::getAssets()
            ->addCss('assets/umeditor/themes/default/css/umeditor.min.css')
            ->addCss('assets/umeditor/xh/xh.formula.css')
            ->addJs('assets/umeditor/third-party/template.min.js')
            ->addJs('assets/umeditor/umeditor.config.js')
            ->addJs('assets/umeditor/umeditor.min.js')
            ->addJs('assets/umeditor/lang/zh-cn/zh-cn.js')
            ->addJs('assets/umeditor/xh/xh.formula.js');
        self::importJs($jsName);
    }

    public static function UEditor($jsName = null)
    {
        self::getAssets()
            ->addCss('assets/ueditor/themes/default/css/ueditor.css')
            ->addJs('assets/ueditor/ueditor.config.js')
            ->addJs('assets/ueditor/ueditor.all.js')
            ->addJs('assets/ueditor/lang/zh-cn/zh-cn.js');
        self::importJs($jsName);
    }

    public static function xhChart($jsName = null)
    {
        self::getAssets()
            ->addJs('assets/echarts/echarts.common.min.js')
            ->addJs('assets/xh/xhCharts.js?ver=' . VER);
        self::importJs($jsName);
    }

    public static function viewer($jsName = null)
    {
        self::getAssets()
            ->addCss('assets/viewer/viewer.min.css')
            ->addJs('assets/viewer/viewer.min.js');
        self::importJs($jsName);
    }

    public static function toast($jsName = null)
    {
        self::getAssets()
            ->addCss('assets/toast/jquery.toast.css')
            ->addJs('assets/toast/jquery.toast.js');
        self::importJs($jsName);
    }

    public static function easyUIMobile($jsName = null)
    {

        self::getAssets()
            ->addCss('assets/easyui/themes/material/easyui.css')
            ->addCss('assets/easyui/themes/mobile.css')
            ->addCss('assets/easyui/themes/icon.css')
            ->addJs('assets/easyui/jquery.easyui.min.js')
            ->addJs('assets/easyui/jquery.easyui.mobile.js')
            ->addJs('assets/easyui/easyui-lang-zh_CN.js');
        self::importJs($jsName);
    }

    public static function FroalaEditor($jsName = null)
    {
        self::getAssets()
            ->addCss('assets/froalaeditor/css/froala_editor.min.css')
            ->addCss('assets/froalaeditor/css/froala_style.min.css')
            ->addCss('assets/froalaeditor/css/plugins/colors.min.css')
            ->addCss('assets/froalaeditor/css/plugins/code_view.min.css')
            ->addCss('assets/froalaeditor/css/plugins/image.min.css')
            ->addCss('assets/froalaeditor/css/plugins/line_breaker.min.css')
            ->addCss('assets/froalaeditor/css/plugins/table.min.css')
            ->addJs('assets/froalaeditor/js/froala_editor.min.js')
            ->addJs('assets/froalaeditor/js/plugins/table.min.js')
            ->addJs('assets/froalaeditor/js/plugins/colors.min.js')
            ->addJs('assets/froalaeditor/js/plugins/code_view.min.js')
            ->addJs('assets/froalaeditor/js/plugins/font_family.min.js')
            ->addJs('assets/froalaeditor/js/plugins/font_size.min.js')
            ->addJs('assets/froalaeditor/js/plugins/image.min.js')
            ->addJs('assets/froalaeditor/js/languages/zh_cn.js');
        self::importJs($jsName);
    }

    public static function sweetAlert2($jsName = null)
    {
        self::getAssets()
            ->addCss('assets/sweetalert2/sweetalert2.min.css')
            ->addJs('assets/sweetalert2/sweetalert2.min.js');
        self::importJs($jsName);
    }

    public static function xhFlow($jsName = null)
    {
        self::sweetAlert2();
        self::select2();
        self::getAssets()
            ->addJs('assets/axios/dist/axios.min.js')
            ->addCss('common/flow.css?ver=' . VER)
            ->addJs('common/flow.js?ver=' . VER);
        self::importJs($jsName);
    }

    public static function flexSlider2($jsName = null)
    {
        self::getAssets()
            ->addCss('assets/flexSlider2/flexslider.css')
            ->addJs('assets/flexSlider2/jquery.flexslider.js');
        self::importJs($jsName);
    }

    public static function flatUI($jsName = null)
    {
        self::getAssets()
            ->addCss('assets/flatui/flat-ui.css')
            ->addJs('assets/flatui/flat-ui.js');
        self::importJs($jsName);
    }

    public static function FullCalendar($jsName = null)
    {
        self::getAssets()
            ->addCss('assets/fullcalendar/fullcalendar.min.css')
            //     ->addCss('assets/fullcalendar/fullcalendar.print.min.css')
            // ->addJs('assets/fullcalendar/lib/jquery.min.js')

            ->addJs('assets/fullcalendar/lib/moment.min.js')
            ->addJs('assets/fullcalendar/fullcalendar.min.js')
            ->addJs('assets/fullcalendar/locale-all.js');
        self::importJs($jsName);
    }

    public static function layUI($jsName = null, $all = false)
    {
        self::getAssets()
            ->addCss('assets/layui2/css/layui.css')
            ->addJs('assets/layui2/' . ($all ? 'layui.all.js' : 'layui.js'));
        self::importJs($jsName);
    }

    /**
     * 部门组织树
     *
     * @param null $jsName
     *
     * create by dale 20180306
     */
    public static function jOrgChart($jsName = null)
    {
        self::getAssets()
            ->addCss('assets/jOrgChart/css/jquery.jOrgChart.css')
            ->addCss('assets/jOrgChart/css/custom.css')
            ->addCss('assets/jOrgChart/css/prettify.css')
            ->addJs('assets/jOrgChart/prettify.js')
            ->addJs('assets/jOrgChart/jquery.jOrgChart.js');
        self::importJs($jsName);
    }

    public static function nthTabs($jsName = null)
    {
        self::getAssets()
            ->addCss('assets/nth-tabs/css/nth.tabs.min.css')
            ->addJs('assets/jquery/jquery.scrollbar.min.js')
            ->addJs('assets/nth-tabs/js/nth.tabs.min.js');
        self::importJs($jsName);
    }


    public static function radialIndicator($jsName = null)
    {
        self::getAssets()
            ->addJs('assets\radialIndicator\radialIndicator.min.js');
        self::importJs($jsName);
    }

    /**
     * html table 直接导出
     * @param null $jsName
     *
     * create by dale  20180807
     */
    public static function excelLentExport($jsName = null)
    {
        self::getAssets()
            ->addJs('assets\excellentexport\excellentexport.js');
        self::importJs($jsName);
    }

    /**
     * 复制粘贴板
     *
     * @param null $jsName
     *
     * create by dale  20180830
     */
    public static function clipboard($jsName = null)
    {
        self::getAssets()
            ->addJs('assets\clipboard\clipboard.js');
        self::importJs($jsName);
    }

    /**
     * 打印
     *
     * @param null $jsName
     *
     * create by qgz 20190124
     */
    public static function jQueryPrint($jsName = null)
    {
        self::getAssets()
            ->addCss('assets/jQueryPrint/css/normalize.min.css')
//            ->addJs('assets/jQueryPrint/dist/jQuery.print.min.js')
            ->addJs('assets/jQueryPrint/js/vendor/html5-3.6-respond-1.1.0.min.js')
            ->addJs('assets/jQueryPrint/jQuery.print.js');
        self::importJs($jsName);
    }
}